package wang.aweb.childcar.controller.api;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.common.Constant;
import wang.aweb.childcar.exception.CarException;
import wang.aweb.childcar.exception.CarExceptionEnum;
import wang.aweb.childcar.filter.UserFilter;
import wang.aweb.childcar.model.pojo.Member;
import wang.aweb.childcar.model.req.AvatarNicknameReq;
import wang.aweb.childcar.model.vo.MemberVO;
import wang.aweb.childcar.service.MemberService;
import wang.aweb.childcar.util.WechatReqUtils;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/api/infoFull/")
public class InfoFullController {
    @Autowired
    private MemberService memberService;
    @Value("${userapp.appid}")
    private String appid;
    @Value("${userapp.secret}")
    private String appsecret;
    @PostMapping("getAllInfo")
    public ApiRestResponse getAllInfo(){
        Integer userId=UserFilter.memberThreadLocal.get().getId();
        //memberService.selectOne(userId);
        MemberVO currentMember=memberService.selectVOById(userId);
        return ApiRestResponse.success(currentMember);
    }
    @PostMapping("index")
    public ApiRestResponse index(@Valid @RequestBody AvatarNicknameReq avatarNicknameReq){

        memberService.updateInfo(avatarNicknameReq);
        return ApiRestResponse.success();
    }
    @PostMapping("setPhone")
    public ApiRestResponse setPhone(@RequestBody Map reqMap){
        if(StringUtils.isEmpty(reqMap.get("code"))){
            throw new CarException(CarExceptionEnum.CODE_NULL);
        }
        JSONObject phoneJson= WechatReqUtils.getPhone(reqMap.get("code").toString(),this.appid,this.appsecret);
        JSONObject infoJson=(JSONObject)phoneJson.get("phone_info");
        String phone=infoJson.get("phoneNumber").toString();
        memberService.setPhone(phone);
        return ApiRestResponse.success(phone);
    }
}
